import requests
import json

def fetch_token(api_url, app_key, app_secret):
    """
    从指定的接口获取 token
    :param api_url: API 的完整 URL
    :param app_key: 应用的 app_key
    :param app_secret: 应用的 app_secret
    :return: 返回的 token 或错误信息
    """
    headers = {
        'Content-Type': 'application/json'
    }

    # 构造请求参数
    payload = {
        'app_key': app_key,
        'app_secret': app_secret
    }

    try:
        # 发送 POST 请求
        response = requests.post(api_url, json=payload, headers=headers)

        # 检查请求是否成功
        if response.status_code == 200:
            response_body = response.json()
            if response_body.get("code") == '0':
                response_data = response_body.get("data")
                return response_data.get("token")  # 返回 token
            else:
                print(f"接口返回错误：{response_body.get('msg')}")
                return None
        else:
            print(f"请求失败，状态码：{response.status_code}")
            return None
    except requests.exceptions.RequestException as e:
        print(f"请求过程中发生错误：{e}")
        return None


def fetch_project_list(api_url, token, prm_biz_scope=None, prm_page_index=None, prm_page_size=None):
    """
    从指定的接口获取 token
    :param api_url: 接口地址
    :param token: token
    :param prm_page_size: ？
    :param prm_page_index: 页索引
    :param prm_biz_scope: 页大小
    :return: 返回的 数据 或错误信息
    """
    headers = {
        'Content-Type': 'application/json'
    }

    # 构造请求参数
    payload = {
        'token': token
    }

    if prm_biz_scope:
        payload['biz_scope'] = prm_biz_scope
    if prm_page_index:
        payload['page_index'] = prm_page_index
    if prm_page_size:
        payload['page_size'] = prm_page_size

    try:
        # 发送 POST 请求
        response = requests.post(api_url, json=payload, headers=headers)

        # 检查请求是否成功
        if response.status_code == 200:
            response_body = response.json()
            if response_body.get("code") == '0':
                response_data = response_body.get("data")
                return response_data  # 返回 data
            else:
                print(f"接口返回错误：{response_body.get('msg')}")
                return None
        else:
            print(f"请求失败，状态码：{response.status_code}")
            return None
    except requests.exceptions.RequestException as e:
        print(f"请求过程中发生错误：{e}")
        return None



def fetch_pv_data_list(api_url, token, station_code , start_time , end_time, prm_page_index=None, prm_page_size=None):
    """
    从
    :param api_url: 接口地址
    :param token: token
    :param station_code: 站点名称
    :param start_time:  起始时间    "2025-03-13 09:00:00"
    :param end_time:  结束时间      "2025-03-13 12:00:00"
    :param prm_page_index: 页索引
    :param prm_page_size: 页数据数量
    :return: 返回的 数据 或错误信息
    """
    headers = {
        'Content-Type': 'application/json'
    }

    # 构造请求参数
    payload = {
        'token': token,
        'station_code' : station_code,
        'start_time' : start_time,
        'end_time' : end_time
    }

    if prm_page_index:
        payload['page_index'] = prm_page_index
    if prm_page_size:
        payload['page_size'] = prm_page_size

    try:
        # 发送 POST 请求
        response = requests.post(api_url, json=payload, headers=headers)

        # 检查请求是否成功
        if response.status_code == 200:
            response_body = response.json()
            if response_body.get("code") == '0':
                response_data = response_body.get("data")
                return response_data  # 返回 data
            else:
                print(f"接口返回错误：{response_body.get('msg')}")
                return None
        else:
            print(f"请求失败，状态码：{response.status_code}")
            return None
    except requests.exceptions.RequestException as e:
        print(f"请求过程中发生错误：{e}")
        return None



